### FIGURES MAIN TEXT


## produces figures displayed in the main text (except for Figure 2, see main.js)
# Figure 1, 3


# Session info
# R version 4.1.0 (2021-05-18)
# Platform: x86_64-apple-darwin17.0 (64-bit)
# Running under: macOS 13.1


# Load packages -----------------------------------------------------------
# if needed install package first:
#install.packages("tidyverse")

library(tidyverse)

# Load data ---------------------------------------------------------------

rm(list = ls())

# load report-level data
data_rep_level <- read_csv("data/data_rep_level.csv")

# load UN data
meta_pred_un <- read_csv("data/meta_pred_un.csv")

#########################################################



#### MAIN TEXT Figures



#########################################################

########

### Figure 1

########

# IO performance at report level, scatter plot of positive assessment share per report by year
set.seed(1234)
meta_pred_un %>%
  ggplot(aes(year, sentiment), group = IO) +
  geom_jitter(aes(color = year),width = 0.35) +
  ylab('Share of Positive Assessment') +
  xlab("Year") +
  theme(axis.text.x = element_text(size = 9, angle = 30, hjust = 1), legend.position = 'none',
        axis.title.x = element_text(size=14),
        axis.title.y = element_text(size=14),
        plot.title = element_text(size = 16)) +
  facet_wrap(~IO)

ggsave("output/figure1.png", scale = 1, 
       dpi = "retina", width = 27, height = 17, units = "cm")

#############

###  Figure 3

############

# color blind friendly palette
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#0072B2", "#000000")
cbPalette_2 <- c("#E69F00", "#56B4E9", "#009E73", "#0072B2", "#000000")

# Box plot of IEG rating levels against positive assessment share
set.seed(5000) # seed for reproducibility 

data_rep_level %>%
  mutate(IEG_Outcome = factor(IEG_Outcome, levels = c("Highly Unsatisfactory", "Unsatisfactory","Moderately Unsatisfactory", "Moderately Satisfactory", "Satisfactory", "Highly Satisfactory"))) %>%
  ggplot(aes(IEG_Outcome, sentiment), group = IEG_Outcome) +
  geom_point(shape = 13, aes(color = IEG_Outcome), position = "jitter") +
  geom_boxplot(aes(group = IEG_Outcome, alpha = 0.01), outlier.shape = NA) +
  stat_boxplot(aes(group = IEG_Outcome), geom = "errorbar", width = 0.4) +
  ylab('Share of Positive Assessment') +
  xlab("IEG Outcome Rating") +
  theme(axis.text.x = element_text(size = 9, angle = 30, hjust = 1), legend.position = 'none',
        axis.title.x = element_text(size=14),
        axis.title.y = element_text(size=14),
        plot.title = element_text(size = 16)) +
  scale_color_manual(values=cbPalette) 

ggsave("output/figure3.png", scale = 1, 
       dpi = "retina", width = 27, height = 17, units = "cm")



############

### Figure 4

############


# Difference in share of positive assessments across evaluation type 
meta_pred_un %>% 
  ## Filter UN data for program and project evaluation types only
  filter(eval_type != "Institutional",
         eval_type != "Thematic") %>% 
  # standardize evaluation type
  mutate(eval_type = ifelse(eval_type == "Programme", "Program", eval_type),
         eval_type = factor(eval_type)) %>% 
  ggplot(aes(x = reorder(eval_type,sentiment),
             y = sentiment)) +
  geom_boxplot() +
  labs(y = "Positive Assessment Share",
       x = "Evaluation Type")

ggsave("output/figure4.png", scale = 1, 
       dpi = "retina", width = 27, height = 17, units = "cm")


